#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
数据查看脚本
查看和分析爬取的价格数据
"""

import redis
import json
from datetime import datetime
import argparse

def connect_redis():
    """连接Redis"""
    redis_configs = [
        {'host': '192.168.88.130', 'port': 6379, 'password': None},
        {'host': '192.168.88.130', 'port': 6379, 'password': ''},
        {'host': '192.168.88.130', 'port': 6379, 'password': 'redis'},
        {'host': '192.168.88.130', 'port': 6379, 'password': '123456'},
        {'host': '192.168.88.130', 'port': 6379, 'password': 'password'},
    ]
    
    for config in redis_configs:
        try:
            r = redis.Redis(
                host=config['host'],
                port=config['port'],
                password=config['password'],
                decode_responses=True
            )
            r.ping()
            print(f"✓ Redis连接成功: {config['host']}:{config['port']}")
            return r
        except:
            continue
    
    raise Exception("无法连接到Redis")

def view_latest_data(limit=10):
    """查看最新数据"""
    r = connect_redis()
    
    # 获取所有价格数据键
    keys = r.keys('cnhnb_price:*')
    if not keys:
        print("没有找到价格数据")
        return
    
    # 按时间戳排序
    keys.sort(reverse=True)
    
    print(f"\n=== 最新 {min(limit, len(keys))} 条价格数据 ===")
    print(f"总数据量: {len(keys)} 条")
    print("-" * 80)
    
    for i, key in enumerate(keys[:limit]):
        try:
            data = r.get(key)
            if data:
                item = json.loads(data)
                
                # 格式化时间
                crawl_time = item.get('crawl_timestamp', '')
                if crawl_time:
                    try:
                        dt = datetime.fromisoformat(crawl_time.replace('Z', '+00:00'))
                        formatted_time = dt.strftime('%H:%M:%S')
                    except:
                        formatted_time = crawl_time
                else:
                    formatted_time = 'N/A'
                
                print(f"{i+1:2d}. 时间: {formatted_time}")
                print(f"    产品: {item.get('name', 'N/A')}")
                print(f"    价格: {item.get('price_value', 'N/A')} 元/{item.get('unit', 'N/A')}")
                print(f"    原始: {item.get('price_original', 'N/A')[:50]}...")
                print(f"    来源: {item.get('source_url', 'N/A')}")
                print()
                
        except Exception as e:
            print(f"解析数据失败: {e}")
            continue

def view_price_statistics():
    """查看价格统计"""
    r = connect_redis()
    
    keys = r.keys('cnhnb_price:*')
    if not keys:
        print("没有找到价格数据")
        return
    
    prices = []
    for key in keys:
        try:
            data = r.get(key)
            if data:
                item = json.loads(data)
                price = item.get('price_value')
                if price and isinstance(price, (int, float)):
                    prices.append(price)
        except:
            continue
    
    if not prices:
        print("没有有效的价格数据")
        return
    
    print(f"\n=== 价格统计 ===")
    print(f"数据条数: {len(prices)}")
    print(f"价格范围: {min(prices):.2f} - {max(prices):.2f} 元")
    print(f"平均价格: {sum(prices)/len(prices):.2f} 元")
    
    # 价格分布
    price_ranges = {
        '0-10元': 0,
        '10-50元': 0,
        '50-100元': 0,
        '100元以上': 0
    }
    
    for price in prices:
        if price < 10:
            price_ranges['0-10元'] += 1
        elif price < 50:
            price_ranges['10-50元'] += 1
        elif price < 100:
            price_ranges['50-100元'] += 1
        else:
            price_ranges['100元以上'] += 1
    
    print("\n价格分布:")
    for range_name, count in price_ranges.items():
        percentage = (count / len(prices)) * 100
        print(f"  {range_name}: {count} 条 ({percentage:.1f}%)")

def view_data_by_time(hours=24):
    """查看指定时间范围内的数据"""
    r = connect_redis()
    
    keys = r.keys('cnhnb_price:*')
    if not keys:
        print("没有找到价格数据")
        return
    
    # 计算时间范围
    now = datetime.now()
    cutoff_time = now.timestamp() - (hours * 3600)
    
    recent_data = []
    for key in keys:
        try:
            data = r.get(key)
            if data:
                item = json.loads(data)
                timestamp = item.get('timestamp', 0)
                if timestamp >= cutoff_time:
                    recent_data.append(item)
        except:
            continue
    
    print(f"\n=== 最近 {hours} 小时的数据 ===")
    print(f"数据条数: {len(recent_data)}")
    
    if recent_data:
        # 按时间排序
        recent_data.sort(key=lambda x: x.get('timestamp', 0))
        
        for i, item in enumerate(recent_data[:10]):  # 显示前10条
            crawl_time = item.get('crawl_timestamp', '')
            if crawl_time:
                try:
                    dt = datetime.fromisoformat(crawl_time.replace('Z', '+00:00'))
                    formatted_time = dt.strftime('%H:%M:%S')
                except:
                    formatted_time = crawl_time
            else:
                formatted_time = 'N/A'
            
            print(f"{i+1:2d}. {formatted_time} - {item.get('price_value', 'N/A')} 元/{item.get('unit', 'N/A')}")

def main():
    parser = argparse.ArgumentParser(description='查看爬取的价格数据')
    parser.add_argument('--latest', '-l', type=int, default=10, help='查看最新N条数据')
    parser.add_argument('--stats', '-s', action='store_true', help='查看价格统计')
    parser.add_argument('--time', '-t', type=int, default=24, help='查看最近N小时的数据')
    
    args = parser.parse_args()
    
    try:
        if args.stats:
            view_price_statistics()
        elif args.time != 24:  # 如果指定了时间参数
            view_data_by_time(args.time)
        else:
            view_latest_data(args.latest)
            
    except Exception as e:
        print(f"错误: {e}")

if __name__ == '__main__':
    main() 